﻿using System;
using System.Linq;
using System.Security.Permissions;
using OS.Infrastructure.Domain;
using OS.Infrastructure.Domain.UnitOfWork;
using TaskKernel.Configuration;
using TaskKernel.Domain;
using TaskKernel.Domain.Interface;

namespace TaskWebApplicationUI.PresenterLayer
{
    public class AdminManagersPresenter : AbstractPresenter<IAdminManagersView>
    {
        private readonly IQueryAccount _queryAccount;
        private readonly IUnitOfWorkFactory _unitOfWork;
        private readonly ICommandRepository<Account> _command;

        public AdminManagersPresenter(IQueryAccount queryAccount,
                                      IUnitOfWorkFactory unitOfWork, ICommandRepository<Account> command)
        {
            _queryAccount = queryAccount;
            _unitOfWork = unitOfWork;
            _command = command;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnAddManager(Guid managerID)
        {
            using (var z = _unitOfWork.Create())
            {
                var account = _queryAccount.Get(View.User);
                var manager = _queryAccount.Get(managerID);
                if (account.AddManager(manager)){
                    _command.Save(account);
                    z.Commit();
                }
            }
            RefreshView();
        }

        public void OnViewInitialized()
        {
            using (var z = _unitOfWork.Create())
            {
                View.Accounts = _queryAccount.GetAllNotFiredAccounts().ToList();
            }
        }

        public void RefreshView()
        {
            using (var z = _unitOfWork.Create())
            {
                var account = _queryAccount.Get(View.User);
                View.Managers = account.MasterAccounts.ToList();
                View.Slavers = account.SlaveAccounts.ToList();
                View.DepartmentName = account.FullName;
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnDeleteManager(Account manager)
        {
            using (var z = _unitOfWork.Create())
            {
                var account = _queryAccount.Get(View.User);
                if (account.RemoveManager(manager)){
                    _command.Save(account);
                    z.Commit();
                }
            }
            RefreshView();
        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnDeleteSlave(Account slaveUser)
        {
            using (var z = _unitOfWork.Create())
            {
                var account = _queryAccount.Get(View.User);
                if (account.RemoveSlaveUser(slaveUser)){
                    _command.Save(account);
                    z.Commit();
                }
            }
            RefreshView();
        }

        [PrincipalPermission(SecurityAction.Demand, Role = TaskKernelConfiguration.AdminRole)]
        public void OnAddSlave(Guid slaveID)
        {
            using (var z = _unitOfWork.Create())
            {
                var account = _queryAccount.Get(View.User);
                var slave = _queryAccount.Get(slaveID);
                if (account.AddSlaveUser(slave)){
                    _command.Save(account);
                    z.Commit();
                }
            }
            RefreshView();
        }
    }
}